import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/Home.vue'
import Detail from '../views/Detail.vue'
import Login from '../views/Login.vue'
import Place from '../views/Place.vue'
// 引入二级路由
import Mymain from '../views/home/main/Mymain.vue'
import Mymove from '../views/home/move/Mymove.vue'
import Myshow from '../views/home/show/Myshow.vue'
import Myvideo1 from '../views/home/video1/Myvideo1.vue'
import Myvideo2 from '../views/home/video2/Myvideo2.vue'
// 引入三级路由
import Hot from '../views/home/move/hot/Hot.vue'
import Cinema from '../views/home/move/cinema/Cinema.vue'
import Classics from '../views/home/move/classics/Classics.vue'
import Plan from '../views/home/move/plan/Plan.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: Home,
    // 从定向到二级路由
    redirect: '/home/move',
    // 配置子路由
    children: [
      {
        path: '/home/move',
        component: Mymove,
        name: 'move',
        // 重定向到三级路由
        redirect: '/home/move/hot',
        children: [
          {
            path: '/home/move/hot',
            name: 'hot',
            component: Hot
          },
          {
            path: '/home/move/cinema',
            name: 'cinem',
            component: Cinema
          },
          {
            path: '/home/move/plan',
            name: 'plan',
            component: Plan
          },
          {
            path: '/home/move/classics',
            name: 'classics',
            component: Classics
          }
        ]
      },
      {
        path: '/home/show',
        name: 'show',
        component: Myshow
      },
      {
        path: '/home/main',
        name: 'main',
        component: Mymain,
        meta: {
          isAute: true
        }
      },
      {
        path: '/home/video1',
        name: 'video1',
        component: Myvideo1
      },
      {
        path: '/home/video2',
        name: 'video2',
        component: Myvideo2
      }
    ]
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/detail',
    name: 'detail',
    component: Detail,
    meta: {
      isAute: true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/place',
    name: 'place',
    component: Place
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to)
  const token = localStorage.getItem('token')
  // 判断是谁要进行拦截
  if (to.meta.isAute) {
    if (token) {
      next()
    } else {
      next({
        path: '/login',
        query: {
          fullPath: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})
export default router
